package com.itheima.controller;

import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.entity.Result;
import cn.itheima.health.service.RoleAdminService;
import com.itheima.pojo.Role;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roleAdmin")
public class RoleAdminController {

    @Reference
    private RoleAdminService roleAdminService;

    @RequestMapping("/findRoleAll.do")
    public Result findRoleAll(){
        try {
            List<Role> list = roleAdminService.findRoleAll();
            return new Result(true,"查询角色成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    @RequestMapping("/findRoleIdsByUserId.do")
    public Result findRoleIdsByUserId(@RequestParam("id")Integer id){
        try {
            Map map = roleAdminService.findRoleIdsByUserId(id);
            return new Result(true,"查询角色成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    @RequestMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map map = roleAdminService.findPage(queryPageBean);
            return new Result(true,"查询角色信息成功",map);
        } catch (Exception e) {
            return new Result(false,"查询角色信息失败");
        }
    }

    @RequestMapping("/add.do")
    public Result add(@RequestBody Role role,@RequestParam("permissionIds")Integer[] permissionIds){
        try {
            roleAdminService.add(role,permissionIds);
            return new Result(true,"新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }

    @RequestMapping("/deleteRoleAdminById")
    public Result deleteRoleAdminById(@RequestParam("id") Integer id){
        try {
            roleAdminService.deleteRoleAdminById(id);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
    }

    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Role role,@RequestParam("permissionIds") Integer[] permissionIds){
        try {
            roleAdminService.edit(role,permissionIds);
            return new Result(true,"编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
    }
}
